﻿using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System;
using OnlineBookReader;
using System.Net;
using HtmlExtractor;
using Reader.Domain;
using System.Linq;
using SLHtmlExtractor.ObjectOutput;
using System.Windows.Markup;
using System.Windows.Media.Imaging;
using System.IO;
using System.Net.Browser;
using System.Xml.Serialization;
using System.Xml;
using System.Runtime.Serialization;
using System.Windows.Media;
namespace Reader.View
{
    [Sample("简介", "bookpage")]
    public partial class BookPageView : OnlineObjectView
    {
        protected override object GenerateObject(string html, string uri)
        {
            return SaveFile.GenerateObject<BookPage>(html, uri);
        }
        protected override void SetElement(object o)
        {
           
            BookPage bp=o as BookPage;
            if (bp != null)
            {
                content.Content = "目录";
                this.DataContext = bp;
                saveBook.IsEnabled = true;
                AddFavorite.IsEnabled = true;
                try
                {
                    if (Context.IsLoacalFile(bp.ImageUri))
                    {
                        ImageTools.Image image = new ImageTools.Image();
                        image.IsLoadingSynchronously = true;
                        image.SetSource(File.OpenRead(bp.ImageUri));
                        
                        bookImage.Source = image;

                    }
                    else
                        //AnimatedImage不能捕获异常？                        
                        bookImage.Source = bp.ImageUri;
                }
                catch (Exception ex)
                {
                }
            }
        }
      
        public BookPageView()
        {
            InitializeComponent();
        }
        public string Uri { get; set; }
        public BookPageView(string uri):this()
        {
            Uri = uri;
        }

       
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            LoadObject(Uri, typeof(BookPage));
           
        }
        private void Img_LoadingFailed(object sender, UnhandledExceptionEventArgs e)
        {

        }
       

        private void lastChapter_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hb=e.OriginalSource as HyperlinkButton;
            SampleBrowser.Current.PopulateSamplesTab("chapter",hb.CommandParameter.ToString());
        }

        private void content_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hb = e.OriginalSource as HyperlinkButton;
            SampleBrowser.Current.PopulateSamplesTab("content", hb.CommandParameter.ToString());
        }

        private void saveBook_Click(object sender, RoutedEventArgs e)
        {
            saveBook.IsEnabled = false;
            SavePage(Uri);
            saveBook.IsEnabled = true;
            
        }
        public void SavePage(string uri)
        {
           BookPage bp= Cache.Current.Get<BookPage>(uri);
           BookPage.SaveXmlFile(Uri);
           LocalBookConfig.LoaclBook.AddBook(bp.BookName, Uri);
           BookContent.SaveXmlFile(bp.BookContentUri,SaveChapter);          
        }
        private void SaveChapter(BookContent bc)
        {
           //todo最后一章要与其他章节区分，最后一章要尝试读取在线最新的内容，不应该读取local缓存。,保存时名称加上lastchapter
            foreach (var chapterUri in bc.Chapters)
            {  
                string template = @"View/Chapter/xamltextchapter.xml";
                BookChapter.SaveChapterXamlFile(chapterUri.Uri, template);
                
            }
           
        }

        private void AddFavorite_Click(object sender, RoutedEventArgs e)
        {
            BookPage bp = Cache.Current.Get<BookPage>(Uri);
            LocalBookConfig.FavoriteBook.AddBook(bp.BookName, Uri);
        }
    }
}
